﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace NFinal.Oauth2
{
    using AppFunc = Func<IDictionary<string, object>,Config, Task>;
    
    public class Config
    {
        public string serviceDir = "/oauth2/";
        public string loginUrl = "login";

        public string qq_oauth2Url = "qqoauth2";
        public string qq_authorizeUrl = "https://api.weibo.com/oauth2/authorize";
        public string qq_redirectUrl = "";
        public string qq_appId = "";
        public string qq_appSecret = "";

        public string sina_authorizeUrl = "https://api.weibo.com/oauth2/authorize";
        public string sina_redirectUrl = "sinaoauth2";
        public string sina_appId = "";
        public string sina_appKey = "";
        

    }
    public class HttpUtility
    {
        public string Get(string url)
        {
            return string.Empty;
        }
        public string Post(string url, string data)
        {
            return string.Empty;
        }
    }
    public class Middleware
    {
        private readonly AppFunc _next;
        private static string rootDir = null;
        public Middleware(AppFunc next)
        {
            this._next = next;
        }
        public Task Invoke(IDictionary<string, object> environment,Config config)
        {
            Microsoft.Owin.IOwinContext context = new Microsoft.Owin.OwinContext(environment);
            context.Response.ContentType = "text/html;charset=utf-8";
            context.Response.StatusCode = 200;
            string requestPath = context.Request.PathBase.Value + context.Request.Path.Value;
            if (requestPath.IndexOf(config.serviceDir) > -1)
            {
                string requestRelativePath = requestPath.Substring(config.serviceDir.Length - 1);
                switch (requestRelativePath)
                {
                    case "images/Connect_logo1.png": { byte[] buffer = ConvertImage(Resource.Connect_logo_1); context.Response.Write(buffer); } break;
                    case "images/Connect_logo2.png": { byte[] buffer = ConvertImage(Resource.Connect_logo_2); context.Response.Write(buffer); } break;
                    case "images/Connect_logo3.png": { byte[] buffer = ConvertImage(Resource.Connect_logo_3); context.Response.Write(buffer); } break;
                    case "images/Connect_logo4.png": { byte[] buffer = ConvertImage(Resource.Connect_logo_4); context.Response.Write(buffer); } break;
                    case "images/Connect_logo5.png": { byte[] buffer = ConvertImage(Resource.Connect_logo_5); context.Response.Write(buffer); } break;
                    case "images/Connect_logo6.png": { byte[] buffer = ConvertImage(Resource.Connect_logo_6); context.Response.Write(buffer); } break;
                    case "images/Connect_logo7.png": { byte[] buffer = ConvertImage(Resource.Connect_logo_7); context.Response.Write(buffer); } break;
                    default: { context.Response.StatusCode = 404; return FromResult<int>(0); }
                }
            }
            return _next.Invoke(environment, null);
        }
        public static byte[] ConvertImage(Image image)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                image.Save(ms, image.RawFormat);
                return ms.ToArray();
            }
        }
        public static Task<TResult> FromResult<TResult>(TResult resultValue)
        {
            var completionSource = new TaskCompletionSource<TResult>();
            completionSource.SetResult(resultValue);
            return completionSource.Task;
        }
    }
}
